<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 外部定义具体动画实现
      const animation = {
        bounce: {
          show(element) {
            console.log(element, "弹跳显示");
          },
          hide(element) {
            console.log(element, "弹跳隐藏");
          },
        },
        slice: {
          show(element) {
            console.log(element, "滑动显示");
          },
          hide(element) {
            console.log(element, "滑动隐藏");
          },
        },
        rotate: {
          show(element) {
            console.log(element, "旋转显示");
          },
          hide(element) {
            console.log(element, "旋转隐藏");
          },
        },
      };

      // 组件提供接口，从外部传入具体实现
      class Modal {
        constructor(element, animation) {
          this.element = element;
          this.animation = animation;
        }

        show() {
          this.animation.show(this.element);
        }

        hide() {
          this.animation.hide(this.element);
        }
      }

      const modal = new Modal("div", animation.slice);
      modal.show();
      modal.hide();
    </script>
  </body>
</html>
